layui.use(['table', 'admin', 'ax'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;

    /**
     * 作业明细表管理
     */
    var TaskDetailed = {
        tableId: "taskDetailedTable"
    };

    /**
     * 初始化表格的列
     */
    TaskDetailed.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键id'},
            {field: 'status', title: '状态',
            	templet: function(d){
            		var str = "";
            		if (d.status == '作业中'){
                    	str += '<span style="color:#ffaa00;font-weight: bold;">' + d.status + '</span>'
					} else if (d.status == '已完成'){
                    	str += '<span style="color:#00c300;font-weight: bold;">' + d.status + '</span>'
					} 
            		return str
            	}},
            {field: 'v2', title: '项目部'},
            {field: 'v1', title: '作业内容'},
            {field: 'detaileds', title: '明细'},
            {field: 'userName', title: '作业人员'},
            {field: 'startTime', title: '开始时间',width:160},
            {field: 'endTime', title: '结束时间', totalRowText: '合计',width:160},
            {field: 'volume', title: '体积', totalRow: true},
            {field: 'weights', title: '重量', totalRow: true},
            {field: 'nums', title: '数量', totalRow: true},
            {field: 'hours', title: '工时', totalRow: true},
        ]];
    };

    // 单击行事件
    table.on('row(' + TaskDetailed.tableId + ')', function (obj) {
        var data = obj.data;
		$('div[lay-id="' + TaskDetailed.tableId + '"]').find(".layui-table-body tr ").attr({ "style": "background:#FFFFFF" });//其他tr恢复原样
		$('div[lay-id="' + TaskDetailed.tableId + '"]').find(obj.tr.selector).attr({ "style": "background:#e6e6e6;" });//改变当前tr颜色
    });

    /**
     * 点击查询按钮
     */
    TaskDetailed.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        table.reload(TaskDetailed.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
            }
        });
    };

    /**
     * 弹出添加对话框
     */
    TaskDetailed.openAddDlg = function () {
        window.location.href = Feng.ctxPath + '/taskDetailed/add';
    };

    /**
     * 导出excel按钮
     */
    TaskDetailed.exportExcel = function () {
        var checkRows = table.checkStatus(TaskDetailed.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    TaskDetailed.openEditDlg = function (data) {
        window.location.href = Feng.ctxPath + '/taskDetailed/edit?id=' + data.id;
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    TaskDetailed.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/taskDetailed/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(TaskDetailed.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + TaskDetailed.tableId,
        url: Feng.ctxPath + '/taskDetailed/sumTaskDetailed?taskId=' + Feng.getUrlParam("taskId"),
        page: true,
        limit: 40,
        height: "full-60",
        totalRow: true,
        toolbar: '#toolbarDemo', //开启头部工具栏，并为其绑定左侧模板
        defaultToolbar: ['filter', 'exports', 'print'],
        cellMinWidth: 100,
        cols: TaskDetailed.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        TaskDetailed.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        TaskDetailed.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        TaskDetailed.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + TaskDetailed.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            TaskDetailed.openEditDlg(data);
        } else if (layEvent === 'delete') {
            TaskDetailed.onDeleteItem(data);
        }
    });
});
